/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.repair.service.impl;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.repair.entity.MaintainPlanContentExt;
import org.springblade.modules.repair.entity.MaintainSign;
import org.springblade.modules.repair.service.IMaintainSignService;
import org.springblade.modules.repair.utils.DateUtils;
import org.springblade.modules.repair.vo.MaintainPlanContentExtVO;
import org.springblade.modules.repair.mapper.MaintainPlanContentExtMapper;
import org.springblade.modules.repair.service.IMaintainPlanContentExtService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 *  服务实现类
 *
 * @author ysq
 * @since 2022-03-28
 */
@Service
public class MaintainPlanContentExtServiceImpl extends BaseServiceImpl<MaintainPlanContentExtMapper, MaintainPlanContentExt> implements IMaintainPlanContentExtService {

	@Autowired
	@Lazy
	private IMaintainSignService signService;
	@Override
	public IPage<MaintainPlanContentExtVO> selectMaintainPlanContentExtPage(IPage<MaintainPlanContentExtVO> page, MaintainPlanContentExtVO maintainPlanContentExt) {
		return page.setRecords(baseMapper.selectMaintainPlanContentExtPage(page, maintainPlanContentExt));
	}

	@Override
	public List<MaintainPlanContentExtVO> maintainPlanList(Long elevatorId) {
		return baseMapper.maintainPlanList(elevatorId);
	}


	@Override
	public void workDurationScript() {
		List<MaintainPlanContentExt> updateExtList = new ArrayList<>();
		List<MaintainPlanContentExt> list = baseMapper.selectFinishByOrderList();
		List<MaintainSign> maintainSignAllList = signService.list();
		Map<Long, List<MaintainSign>> maintainSignMap = maintainSignAllList.stream().filter(o -> o.getPlanId() != null && o.getSignTime() != null && o.getSignOutTime() != null)
			.collect(Collectors.groupingBy(MaintainSign::getPlanId));
		for (MaintainPlanContentExt maintainPlanContentExt : list) {
			List<MaintainSign> maintainSignList = maintainSignMap.get(maintainPlanContentExt.getPlanId());
			if (Func.isEmpty(maintainSignList)) {
				continue;
			}
			long checkTime = maintainSignList.stream().filter(o -> o.getSignOutTime() == null || o.getSignOutTime() == null).filter(o -> o.getSignOutTime().before(o.getSignTime())).count();
			if (checkTime > 0) {
				continue;
			}
			// 计算工单实际作业时长
			List<DateUtils.TimePeriod> periods = maintainSignList.stream()
				.map(maintainSign -> new DateUtils.TimePeriod(maintainSign.getSignTime(), maintainSign.getSignOutTime()))
				.collect(Collectors.toList());

			MaintainPlanContentExt updateExt = new MaintainPlanContentExt();
			updateExt.setId(maintainPlanContentExt.getId());
			updateExt.setWorkDuration(Convert.toInt(DateUtils.calculateWorkDuration(periods)));
			updateExtList.add(updateExt);
		}

		// 分批处理
		List<List<MaintainPlanContentExt>> lists = ListUtil.partition(updateExtList, 1000);
		lists.forEach(item -> {
			this.updateBatchById(item);
		});
	}
}
